/**
 * Created by 蒯越 on 2017/2/22.
 */
angular.module('myApp')

    .controller('queryCtrl', ['$scope', '$http', '$httpParamSerializerJQLike', '$log', 'OpenService', 'CheckboxService',
        'CacheService', 'dateFilter', 'myConstant', 'DateCalculateService', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                                                                      CacheService, dateFilter, myConstant, DateCalculateService) {
            //视图层变量viewModel
            var vm = $scope.vm = {};
            vm.constant = myConstant;
            vm.downCfg = {
                contentType: 'jqLike',
                date: {
                    'oDate': 'yyyyMMdd'
                }
            };
            //变量初始化
            //分页数据
            //vm.noneTime=newDate();
            vm.pagination = {
                pageSize: 10,
                pageNum: 1
            };
            //查询条件
            vm.queryBean = {
                oDate: DateCalculateService.getYesterday()
            };

            vm.validateOptions = {
                blurTrig: true,
                showError: function (elem, errorMessages) {
                    // growl.addErrorMessage(errorMessages);
                    // angular.element(elem).after('<span>' + errorMessages + '</span>');
                },
                removeError: true
            };

            vm.queryDetail = queryDetail;
            vm.resetForm = resetForm;
            vm.create = create;
            vm.busiHandle = busiHandle;
            vm.busiAuditPass = busiAuditPass;
            vm.busiAuditNoPass = busiAuditNoPass;
            vm.auditRollBack = auditRollBack;
            vm.tasterMchnt = tasterMchnt;
            vm.mchntUpdate = mchntUpdate;
            //vm.print = print;
            //vm.newDate = newDate;

            vm.cached = {
                BANK_INFO: {},
                COMANY_CODE: {},
                MERCHANT_CODE: {}
            };

            CacheService.initCache(vm.cached);

            /*将前面缓存的数据格式化
             [{key:value},{key:value}]*/
            vm.getCache = function (key) {
                return CacheService.getCache(key);
            };

            //获取对象数据[user,user,user]
            vm.getObj = function (key, input) {
                return $http.post(config.ctx + '/base/cache/getAll',
                    $httpParamSerializerJQLike({cacheKey: key}))
                    .then(function (response) {
                        return limitToFilter(filterFilter(response.data.object, input), 6);
                    }, function (response) {
                        $log.error('获取数据%s失败', cacheKey);
                    })
            };

            function queryDetail() {
                var queryForm = document.getElementById('queryForm');
                vm.url = angular.element(queryForm).prop('action');

                //组织参数：查询条件 + 分页数据
                vm.postData = vm.queryBean;
                vm.postData.sDate = dateFilter(vm.queryBean.oDate, 'yyyyMMdd');
                $http.post(vm.url, $httpParamSerializerJQLike(vm.postData))
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            vm.pagination.list = data.object;
                            vm.sumObject = data.sumObject;
                            //清空选中记录
                            CheckboxService.clearChecked();
                        } else {
                            alert(data.msg);
                        }
                    });
                // 计算汇总值
                //for (var i = 0; i < 6; i++) {
                //    alert($(".count" + i).text())
                //}
            }

            function resetForm() {
                vm.queryBean = {
                    oDate: DateCalculateService.getYesterday(),
                };
            }

           /* function print(){
                if (!confirm("是否确认此操作?")) {
                    return;
                }
                pagesetup_null();
                vm.noneTime =newDate();//更新时间
                /!*var td = $("#printdiv tr td");
                for(var item in td.length-1){
                    console.info(item);
                    if(td[item].className.indexOf("ng-hide")>=0){
                       td[item].remove();
                        //td[item].style.display="none";
                        //td[item].setAttribute("ng-style", "myObj");

                    };
                }*!/
                $("#printdiv").css("display","block");
                try{
                    $("#printdiv").jqprint()
                }catch (eer){
                    alert("打印失败"+eer);
                    return ;
                }
                //进后台
                $http.post('busiPrint', $httpParamSerializerJQLike(
                    {'settleDate': "null"}))
                    .then(function (response) {
                    }, function (response) {

                    });
                $("#printdiv").css("display","none");


            }
            function pagesetup_null(){
                var     hkey_root,hkey_path,hkey_key;
                hkey_root="HKEY_CURRENT_USER"
                hkey_path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
                try{
                    var RegWsh = new ActiveXObject("WScript.Shell");
                    hkey_key="header";
                    RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
                    hkey_key="footer";
                    RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");
                }catch(e){}
            }

            function newDate(){
                var date = new Date();
                var y = date.getFullYear();
                var m = date.getMonth() + 1;
                m = m < 10 ? ('0' + m) : m;
                var d = date.getDate();
                d = d < 10 ? ('0' + d) : d;
                var h = date.getHours();
                var minute = date.getMinutes();
                minute = minute < 10 ? ('0' + minute) : minute;
                return y + '-' + m + '-' + d+' '+h+':'+minute;
            }*/
            function create() {
                if (!confirm("是否确认此操作?")) {
                    return;
                }
                $http.post('create', $httpParamSerializerJQLike(
                    {'settleDate': dateFilter(vm.queryBean.oDate, 'yyyyMMdd')}))
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert(data.object);
                            queryDetail();
                        } else {
                            alert(data.msg);
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function busiHandle(reportNo) {
                if (!confirm("是否确认此操作?")) {
                    return;
                }
                $http.post('busiHandle', $httpParamSerializerJQLike(
                    {'reportNo': reportNo}))
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert(data.object);
                            queryDetail();
                        } else {
                            alert(data.msg);
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function tasterMchnt(reportNo) {
                if (!confirm("是否确认此操作?")) {
                    return;
                }
                $http.post('tasterMchnt', $httpParamSerializerJQLike(
                    {'reportNo': reportNo}))
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert(data.object);
                            queryDetail();
                        } else {
                            alert(data.msg);
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function busiAuditPass(reportNo) {
                if (!confirm("是否确认此操作?")) {
                    return;
                }
                $http.post('busiAuditPass', $httpParamSerializerJQLike(
                    {'reportNo': reportNo}))
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert(data.object);
                            queryDetail();
                        } else {
                            alert(data.msg);
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function busiAuditNoPass(reportNo) {
                if (!confirm("是否确认此操作?")) {
                    return;
                }
                $http.post('busiAuditNoPass', $httpParamSerializerJQLike(
                    {'reportNo': reportNo}))
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert(data.object);
                            queryDetail();
                        } else {
                            alert(data.msg);
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function auditRollBack(reportNo) {
                if (!confirm("是否确认此操作?")) {
                    return;
                }
                $http.post('auditRollBack', $httpParamSerializerJQLike(
                    {'reportNo': reportNo}))
                    .then(function (response) {
                        var data = response.data;
                        if (data.success) {
                            alert(data.object);
                            queryDetail();
                        } else {
                            alert(data.msg);
                        }
                    }, function (response) {
                        alert(response.statusText);
                        $log.debug("error");
                    });
            }

            function mchntUpdate(dayBalanceReportData){
                if(dayBalanceReportData.show == false){
                    dayBalanceReportData.show = true;
                    dayBalanceReportData.dateNotMatchAmt = parseFloat(Big(dayBalanceReportData.dateNotMatchAmt).div(100));
                    dayBalanceReportData.mchntLaterAmt = parseFloat(Big(dayBalanceReportData.mchntLaterAmt).div(100));
                    dayBalanceReportData.payMoreAmt = parseFloat(Big(dayBalanceReportData.payMoreAmt).div(100));
                    dayBalanceReportData.payBeforeAmt = parseFloat(Big(dayBalanceReportData.payBeforeAmt).div(100));
                }else {
                    dayBalanceReportData.dateNotMatchAmt = parseFloat(Big(dayBalanceReportData.dateNotMatchAmt).times(100));
                    dayBalanceReportData.mchntLaterAmt = parseFloat(Big(dayBalanceReportData.mchntLaterAmt).times(100));
                    dayBalanceReportData.payMoreAmt = parseFloat(Big(dayBalanceReportData.payMoreAmt).times(100));
                    dayBalanceReportData.payBeforeAmt = parseFloat(Big(dayBalanceReportData.payBeforeAmt).times(100));
                    var amt1 = parseFloat(Big(dayBalanceReportData.correspondOrgAmtC).minus(dayBalanceReportData.correspondOrgAmtD));
                    var amt2 = parseFloat(Big(dayBalanceReportData.mchntTransAmtC).minus(dayBalanceReportData.mchntTransAmtD).minus(dayBalanceReportData.dateNotMatchAmt)
                        .plus(dayBalanceReportData.payMoreAmt).minus(dayBalanceReportData.mchntLaterAmt).plus(dayBalanceReportData.payBeforeAmt));
                    if(!Big(amt1).eq(amt2)){
                        dayBalanceReportData.dateNotMatchAmt = parseFloat(Big(dayBalanceReportData.dateNotMatchAmt).div(100));
                        dayBalanceReportData.mchntLaterAmt = parseFloat(Big(dayBalanceReportData.mchntLaterAmt).div(100));
                        dayBalanceReportData.payMoreAmt = parseFloat(Big(dayBalanceReportData.payMoreAmt).div(100));
                        dayBalanceReportData.payBeforeAmt = parseFloat(Big(dayBalanceReportData.payBeforeAmt).div(100));
                        alert("商户侧T日应计数据与商户侧T日销账明细不相等！");
                        return;
                    }
                    dayBalanceReportData.show = false;

                    if (!confirm("是否确认此操作?")) {
                        return;
                    }
                    $http.post('mchntUpdate',dayBalanceReportData,config.jsonHeader)
                        .then(function (response) {
                            var data = response.data;
                            if (data.success) {
                                alert(data.object);
                                queryDetail();
                            } else {
                                alert(data.msg);
                            }
                        }, function (response) {
                            alert(response.statusText);
                            $log.debug("error");
                        });

                }
            }
        }]);
